package main.Daoimpl;

/**
 * 作者: ALIM-MASTIK
 * 创建实现: 2023/7/15 9:06
 * 项目名称：chat
 * 软件: IntelliJ IDEA
 */

import main.dao.ChatMessageDAO;
import main.pojo.ChatMessage;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChatMessageDAOImpl implements ChatMessageDAO {

    private Connection connection;
    public ChatMessageDAOImpl(Connection connection) {
        this.connection = connection;
    }
    public ChatMessageDAOImpl() {}

    @Override
    public ChatMessage getChatMessageByID(int messageID) {
        ChatMessage chatMessage = null;
        String query = "SELECT * FROM chatmessages WHERE MessageID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, messageID);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                chatMessage = extractChatMessageFromResultSet(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return chatMessage;
    }

    @Override
    public ChatMessage getChatMessageBySenderIDAndReceiverID(int senderID, int receiverID) {
        ChatMessage chatMessage = null;
        String query = "SELECT * FROM chatmessages WHERE SenderID = ? AND ReceiverID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, senderID);
            preparedStatement.setInt(2, receiverID);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                chatMessage = extractChatMessageFromResultSet(resultSet);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return chatMessage;
    }

    @Override
    public List<ChatMessage> getChatMessagesByUser(int userID) {
        List<ChatMessage> chatMessages = new ArrayList<>();
        String query = "SELECT * FROM chatmessages WHERE SenderID = ? OR ReceiverID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, userID);
            preparedStatement.setInt(2, userID);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                chatMessages.add(extractChatMessageFromResultSet(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return chatMessages;
    }

    @Override
    public List<ChatMessage> getChatMessagesByChatRoom(int chatRoomID) {
        List<ChatMessage> chatMessages = new ArrayList<>();
        String query = "SELECT * FROM chatmessages WHERE ChatRoomID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, chatRoomID);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                chatMessages.add(extractChatMessageFromResultSet(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return chatMessages;
    }

    @Override
    public List<ChatMessage> getAllChatMessages() throws SQLException {
        List<ChatMessage> chatMessages = new ArrayList<>();
        String query = "SELECT * FROM chatmessages";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                chatMessages.add(extractChatMessageFromResultSet(resultSet));
            }
        }
        return chatMessages;
    }

    @Override
    public Boolean createChatMessage(ChatMessage chatMessage) {
        String query = "INSERT INTO chatmessages (SenderID, ReceiverID, ChatRoomID, MessageType, MessageContent, SendTime, photo) VALUES (?, ?, ?, ?, ?, ?, ?)";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, chatMessage.getSenderID());
            preparedStatement.setInt(2, chatMessage.getReceiverID());
            preparedStatement.setInt(3, chatMessage.getChatRoomID());
            preparedStatement.setString(4, chatMessage.getMessageType());
            preparedStatement.setString(5, chatMessage.getMessageContent());
            preparedStatement.setTimestamp(6, new java.sql.Timestamp(chatMessage.getSendTime().getTime()));
            preparedStatement.setBytes(7, chatMessage.getPhoto());
            int rowsInserted = preparedStatement.executeUpdate();
            return rowsInserted > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean updateChatMessage(ChatMessage chatMessage) {
        String query = "UPDATE chatmessages SET SenderID = ?, ReceiverID = ?, ChatRoomID = ?, MessageType = ?, MessageContent = ?, SendTime = ?, photo = ? WHERE MessageID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, chatMessage.getSenderID());
            preparedStatement.setInt(2, chatMessage.getReceiverID());
            preparedStatement.setInt(3, chatMessage.getChatRoomID());
            preparedStatement.setString(4, chatMessage.getMessageType());
            preparedStatement.setString(5, chatMessage.getMessageContent());
            preparedStatement.setTimestamp(6, new java.sql.Timestamp(chatMessage.getSendTime().getTime()));
            preparedStatement.setBytes(7, chatMessage.getPhoto());
            preparedStatement.setInt(8, chatMessage.getMessageID());
            int rowsUpdated = preparedStatement.executeUpdate();
            return rowsUpdated > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean deleteChatMessage(int messageID) {
        String query = "DELETE FROM chatmessages WHERE MessageID = ?";
        try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
            preparedStatement.setInt(1, messageID);
            int rowsDeleted = preparedStatement.executeUpdate();
            return rowsDeleted > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    private ChatMessage extractChatMessageFromResultSet(ResultSet resultSet) throws SQLException {
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setMessageID(resultSet.getInt("MessageID"));
        chatMessage.setSenderID(resultSet.getInt("SenderID"));
        chatMessage.setReceiverID(resultSet.getInt("ReceiverID"));
        chatMessage.setChatRoomID(resultSet.getInt("ChatRoomID"));
        chatMessage.setMessageType(resultSet.getString("MessageType"));
        chatMessage.setMessageContent(resultSet.getString("MessageContent"));
        chatMessage.setSendTime(resultSet.getTimestamp("SendTime"));
        chatMessage.setPhoto(resultSet.getBytes("photo"));
        return chatMessage;
    }
}